// $Id$
//////////////////////////////////////////////////////////////////////
// Purpose : To find out a file statu from a file discriptor
//////////////////////////////////////////////////////////////////////

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>

int main(int argc, char **argv)
{
	int fd;
	int statu;
	int access;

	if (argc != 2)
	{
		printf( "Need a fd arg.\n" );
		return -1;
	}

	fd = (int)strtol(argv[1], NULL, 10);	

	if (errno)
	{
		perror(argv[0]);
		return -1;
	}

	// Get the file statu
	statu = fcntl(fd, F_GETFL);
	access = statu & O_ACCMODE;

	switch(access)
	{
		case O_RDONLY:
			printf("The file discriptor %d is readonly.\n", fd);
			break;
		case O_WRONLY:
			printf("The file discriptor %d is writeonly.\n", fd);
			break;
		case O_RDWR:
			printf("The file discriptor %d is read write.\n", fd);
			break;
		default:
			printf("The file discriptor %d error.\n", fd);
			perror(argv[0]);
			return -1;
	}

#if defined(O_SYNC)
	if (statu & O_SYNC)
	{
		printf("The file discriptor %d synchronous writes.\n", fd);
	}
#endif

	if (statu & O_NONBLOCK)
	{
		printf("The file discriptor %d is noblocking.\n", fd);
	}

	if (statu & O_APPEND)
	{
		printf("The file discriptor %d is append.\n", fd);
	}

#ifdef O_FSYNC
	if (statu & O_FSYNC)
	{
		printf("The file discriptor %d synchronous writes.\n", fd);
	}
#endif

	return 0;
}
